package com.nongcai159.mall.lib_common.utils

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.PackageManager.NameNotFoundException
import android.text.TextUtils
import com.nongcai159.mall.lib_common.base.BaseApplication

/**
 * 配置相关
 *
 *
 * Created by tanbiheng on 2017/9/19.
 */

object AppConfig {
    // 版本号string
    private var versionChar: String? = null
    // 版本号int
    private var versionInt = -1
    // 渠道号
    private var channel: String? = null

    /**
     * 版本号内容(字符串)
     *
     * @return
     */
    val versionChars: String
        get() {
            if (versionChar == null) {
                var packageManager = BaseApplication.INSTANCE!!.getPackageManager()
                var packageInfo: PackageInfo? = null
                try {
                    packageInfo = packageManager!!.getPackageInfo(BaseApplication.INSTANCE!!.getPackageName(), 0)
                } catch (e: NameNotFoundException) {
                    e.printStackTrace()
                }

                if (packageInfo == null) {
                    return "1.0"
                }
                versionChar = packageInfo.versionName

                packageInfo = null
                packageManager = null

                if (versionChar == null) {
                    versionChar = "1.0"
                }
            }

            return versionChar!!
        }

    /**
     * 获取渠道号
     *
     * @return
     */
    //获取app当前的渠道号
    val appChannel: String?
        get() {
            if (channel == null) {
                channel = getAppMetaData(BaseApplication.INSTANCE!!,
                        "UMENG_CHANNEL")
            }

            return channel
        }

    /**
     * 版本号(整型)
     *
     * @return
     */
    fun getVersionInt(): Int {
        if (versionInt == -1) {
            var packageManager = BaseApplication.INSTANCE!!.getPackageManager()
            var packageInfo: PackageInfo? = null
            try {
                packageInfo = packageManager!!.getPackageInfo(BaseApplication.INSTANCE!!.getPackageName(), 0)
            } catch (e: NameNotFoundException) {
                e.printStackTrace()
            }

            if (packageInfo == null) {
                return 1
            }

            versionInt = packageInfo.versionCode

            packageInfo = null
            packageManager = null
        }

        return versionInt
    }

    /**
     * 获取app当前的渠道号或application中指定的meta-data
     *
     * @return 如果没有获取成功(没有对应值，或者异常)，则返回值为空
     */
    private fun getAppMetaData(context: Context?, key: String): String? {
        if (context == null || TextUtils.isEmpty(key)) {
            return null
        }
        var channel: String? = null
        try {
            val packageManager = context.packageManager
            if (packageManager != null) {
                val applicationInfo = packageManager.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA)
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        channel = applicationInfo.metaData.getString(key)
                    }
                }
            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }

        return channel
    }
}
